package com.zhongyu.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyu.mapper.LogMapper;
import com.zhongyu.mapper.ProviderMapper;
import com.zhongyu.pojo.Log;
import com.zhongyu.pojo.Provider;
import com.zhongyu.pojo.User;
import com.zhongyu.service.ProviderService;
import com.zhongyu.vo.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Service("providerService")
public class ProviderServiceImpl extends ServiceImpl<ProviderMapper, Provider> implements ProviderService {
    @Resource
    private Log log;
    @Resource
    private ProviderMapper providerMapper;
    @Resource
    private LogMapper logMapper;
    @Override
    public PageResult searchAll(Integer pageNo, Integer pageSize, Provider provider) {
        LambdaQueryWrapper<Provider> wrapper= Wrappers.<Provider>lambdaQuery();
        wrapper.like(!StringUtils.isEmpty(provider.getProCode()), Provider::getProCode,provider.getProCode())
                .like(!StringUtils.isEmpty(provider.getProName()), Provider::getProName,provider.getProName())
                .like(!StringUtils.isEmpty(provider.getProDesc()), Provider::getProDesc,provider.getProDesc())
                .like(!StringUtils.isEmpty(provider.getProContact()), Provider::getProContact,provider.getProContact())
                .like(!StringUtils.isEmpty(provider.getProPhone()), Provider::getProPhone,provider.getProPhone())
                .like(!StringUtils.isEmpty(provider.getProAddress()), Provider::getProAddress,provider.getProAddress())
                .like(!StringUtils.isEmpty(provider.getProFax()), Provider::getProFax,provider.getProFax());
        Page page=new Page(pageNo,pageSize);
        List<Provider> list=providerMapper.findAll(page,wrapper);
        return new PageResult(list,page.getTotal());

    }

    @Override
    public void add(Provider provider) {
        providerMapper.insert(provider);
    }

    @Override
    public void del(Integer id) {
        providerMapper.deleteById(id);
    }

    @Override
    public void update(Provider provider) {
        providerMapper.updateById(provider);

    }
    public void batchDel(Integer[] ids) {
        System.out.println("ServiceImpl批量删除"+ids);
        providerMapper.batchDel(ids);
    }

    @Override
    public List<Provider> searchZeng(Integer[] ids,Provider provider, HttpSession session) {
       List<Provider> list=  providerMapper.search(ids);
        User loginUser= (User) session.getAttribute("loginUser");
        for (int i=0;i<list.size();i++){
            logMapper.zeng("供应商信息-名称:"+list.get(i).getProName()+"，  联系人:"+list.get(i).getProContact()+"，  联系电话:"+list.get(i).getProPhone(),provider.getYuanYin(),log.setType(Log.TYPE_SHAN),loginUser.getId(), LocalDateTime.now());
        }
      return list;

    }

    @Override
    public List<Provider> searchAll() {
        return providerMapper.selectList(null);
    }
}
